package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuMapper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;



/**
 * @author: jwf
 * @date ：Created in 2021/3/12
 * @description ：
 * @version: 1.0
 */
@DubboService
@Slf4j
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //1.分页
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //2.组装查询条件
        String queryString = queryPageBean.getQueryString();
        Example example = new Example(Menu.class);
        Example.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(queryString)){
            criteria.orLike("code", "%" + queryString + "%");
            criteria.orLike("name", "%" + queryString + "%");
            criteria.orLike("helpCode", "%" + queryString + "%");
        }
        //3.查询
        Page<Menu> page = (Page<Menu>)menuMapper.selectByExample(example);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Menu menu) {
        log.info("[菜单---新增]data: {}", menu);
        menuMapper.insertSelective(menu);
    }

    @Override
    public void edit(Menu menu) {
        log.info("[菜单-根据名称修改数据]data: {}", menu);
        menuMapper.updateByPrimaryKeySelective(menu);
    }

    @Override
    public Menu findById(Integer id) {
        log.info("[菜单-根据id查询]data: {}", id );
        return menuMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteById(Integer id) {
        log.info("[菜单-根据id删除]data: {}", id );
        menuMapper.deleteByPrimaryKey(id);
    }
}
